'''
Created on Jan 21, 2013

@author: peng
'''
import sys
import simplejson


def do_filter(log):
    '''log parse core'''
    values = log.split('\t')
    process_log = simplejson.loads(values[-1])
    for l in process_log:
        if l[0] == 'RawTrackData':
            raw_id = l[1]
            raw_hash = l[2]
            print str(raw_id) + ',' ,
            return {'id': raw_id, 'hash': raw_hash}
    return None


def main():
    '''main process'''
    if len(sys.argv) == 1:
        exit(1)
    file_path = sys.argv[1]
    result = []
    log_file = open(file_path, 'r')
    for l in log_file:
        filtered_log = do_filter(l)
        if filtered_log:
            result.append(filtered_log)
        else:
            print 'Not Found!'
    log_file.close()

    result_path = file_path + '_result.data'
    result_file = open(result_path, 'w')
    result_file.write(simplejson.dumps(result))
    result_file.close()


if __name__ == '__main__':
    main()
